package com.vito.oa.act.extend.action;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.vito.act.model.ActExBussModel;
import com.vito.act.service.ActivitiUtilService;
import com.vito.base.authoriza.action.BaseAction;
import com.vito.base.authoriza.cache.SessionMap;
import com.vito.base.authoriza.model.UserModel;
import com.vito.base.model.ResMsg;
import com.vito.base.model.Result;
import com.vito.base.util.Contants;
import com.vito.base.util.Validata;
import com.vito.oa.act.constant.OaActContants;
import com.vito.oa.act.extend.model.OaActEExBussDefModel;
import com.vito.oa.act.extend.model.OaActExBussModel;
import com.vito.oa.act.extend.service.OaActExBussService;

@Controller
@RequestMapping("oa/act/actExBuss")
public class OaActExBussAction extends BaseAction{
	private static final Logger log = LoggerFactory.getLogger(OaActExBussAction.class);
	@Reference(group="OaActExBussServiceImpl")
	private OaActExBussService actExBussService;
	@Reference
	private ActivitiUtilService activitiUtilService;

	@RequestMapping("saveActExBuss")
	@ResponseBody
	public Result saveActExBuss(OaActExBussModel actExBussModel){
		Result rs = new Result();
		try{
			UserModel user = SessionMap.getObj();
			if(user == null || Validata.isNullOrEmpty(user.getUserId())){
				rs.setCode(Result.CODE_LOGIN_REQUIRE);
				rs.setMsg(ResMsg.MSG_LOGIN_NEED);
				return rs;
			}
			String processKey = actExBussModel.getProcessKey();
			int exist_count = actExBussService.queryActExBussExistCount(processKey);
			if(exist_count>0){
				rs.setCode(Result.CODE_PARA_ERROR);
				rs.setMsg("该流程已存在！");
				return rs;
			}
			actExBussModel.setCreateUser(user.getUserId());
			actExBussModel.setCreateDept(user.getDeptParentId());
			actExBussModel.setIsDefined(OaActContants.BASE_STATE_DIS);
			Object obj = activitiUtilService.getAllTaskNode(processKey);
			actExBussService.saveActExBuss(actExBussModel,(List)obj);
			rs.setCode(Result.CODE_OK);
			rs.setMsg(ResMsg.SAVESUC);
		
		}catch(Exception e){
			log.error("添加异常：",e);
			rs.setCode(Result.CODE_ERROR);
		}
		return rs;
	}
	@RequestMapping("updateActExBuss")
	@ResponseBody
	public Result updateActExBuss(OaActExBussModel actExBussModel){
		Result rs = new Result();
		try{
			UserModel user = SessionMap.getObj();
			if(user == null || Validata.isNullOrEmpty(user.getUserId())){
				rs.setCode(Result.CODE_LOGIN_REQUIRE);
				rs.setMsg(ResMsg.MSG_LOGIN_NEED);
				return rs;
			}
			if(user.getUserType()>=Contants.ADMIN_USER_TYPE){
				int count = actExBussService.updateActExBuss(actExBussModel);
				if(count>0){
					rs.setCode(Result.CODE_OK);
					rs.setMsg(ResMsg.UPDSUC);
				}else{
					rs.setCode(Result.CODE_OK);
					rs.setMsg(ResMsg.UPDERR);
				}
			}else{
				rs.setCode(Result.CODE_ERROR);
				rs.setMsg("您无权限进行此操作");
			}
		}catch(Exception e){
			log.error("更新异常：",e);
			rs.setCode(Result.CODE_ERROR);
		}
		return rs;
	}
	@RequestMapping("deleteActExBuss")
	@ResponseBody
	public Result deleteActExBuss(ActExBussModel actExBussModel){
		Result rs = new Result();
		try{
			UserModel user = SessionMap.getObj();
			if(user == null || Validata.isNullOrEmpty(user.getUserId())){
				rs.setCode(Result.CODE_LOGIN_REQUIRE);
				rs.setMsg(ResMsg.MSG_LOGIN_NEED);
				return rs;
			}
			if(user.getUserType()>=Contants.ADMIN_USER_TYPE){
				int count = actExBussService.deleteActExBuss(actExBussModel);
				if(count>0){
					rs.setCode(Result.CODE_OK);
					rs.setMsg(ResMsg.DELSUC);
				}else{
					rs.setCode(Result.CODE_OK);
					rs.setMsg(ResMsg.DELERR);
				}
			}else{
				rs.setCode(Result.CODE_ERROR);
				rs.setMsg("您无权限进行此操作");
			}
			
		}catch(Exception e){
			log.error("流程删除异常：",e);
			rs.setCode(Result.CODE_ERROR);
		}
		return rs;
	}
	
	@RequestMapping("queryActExBussByPage")
	@ResponseBody
	public Result queryActExBussByPage(OaActExBussModel actExBussModel){
		Result rs = new Result();
		try{
			UserModel user = SessionMap.getObj();
			if(user == null || Validata.isNullOrEmpty(user.getUserId())){
				rs.setCode(Result.CODE_LOGIN_REQUIRE);
				rs.setMsg(ResMsg.MSG_LOGIN_NEED);
				return rs;
			}
			actExBussModel.setCreateDept(user.getDeptParentId());
			actExBussModel.setPageSize(10000);
			actExBussModel.setMoudelType(OaActContants.PROCESS_MOUDEL_TYPE_APPROVAL);
			rs.setData(actExBussService.queryActExBuss(actExBussModel));
			rs.setCode(Result.CODE_OK);
		}catch(Exception e){
			log.error("流程查询异常：",e);
			rs.setCode(Result.CODE_ERROR);
		}
		return rs;
	}
	
	@RequestMapping("queryActBussDefByPage")
	@ResponseBody
	public Result queryActBussDefByPage(OaActEExBussDefModel actEExBussDefModel){
		Result rs = new Result();
		try{
			UserModel user = SessionMap.getObj();
			if(user == null || Validata.isNullOrEmpty(user.getUserId())){
				rs.setCode(Result.CODE_LOGIN_REQUIRE);
				rs.setMsg(ResMsg.MSG_LOGIN_NEED);
				return rs;
			}
			actEExBussDefModel.setDeptId(user.getDeptParentId());
			rs.setData(actExBussService.queryActBussDef(actEExBussDefModel));
			rs.setCode(Result.CODE_OK);
		}catch(Exception e){
			log.error("流程节点查询异常：",e);
			rs.setCode(Result.CODE_ERROR);
		}
		return rs;
	}

}
